<template>
	<view class="contont">
		<u-loading-page :loading="loading" style="z-index: 10"></u-loading-page>
		<view class="tutor-intro">
			<p data-v-6effc544="">资深易学研究者<em data-v-6effc544="">罗禹老师</em>根据 你的生日<em data-v-6effc544="">量身定制</em>的姻缘分析
				报告，内容涵盖姻缘方方面 面，已有<em data-v-6effc544="">247353</em>人购
				买，好评率高达<em data-v-6effc544="">95.43%</em>！</p>
			<img src="/static/images/logo/imageTeacher.png" class="img">
		</view>
		<view class="save-result">
			<view class="heading">
				<h3 style="font-size: .3rem;font-weight: 400;">请保存你的付费结果</h3>
			</view>
			<view>
				<strong class="title">保存测算结果</strong>
				<uni-easyinput placeholder="输入手机号保存" v-model="poneNumber" type="number" :clearable="false"
					class="input">
				</uni-easyinput>
				<view style="display: flex;align-items: center;justify-content: center">
					<small class="smalls">*仅支持中国大陆手机号</small>
				</view>
				<view class="adress-qr-adress" v-if="seens">
					<uv-qrcode ref="qrcode" size="160px" :value="qrcode"></uv-qrcode>
					<view>使用微信扫描获取结果 ~</view>
				</view>
				<u-button class="button" @click="savePhone">领取测算并查看结果</u-button>
				<view class="agree">
					<view class="icon-agree"></view>
					我同意自动注册并关联购买记录
				</view>
			</view>
		</view>
		<view class="steps">
			<ul>
				<li class="step">

					<img data-v-6effc544=""
						src="">
					付款成功
				</li>
				<li class="next">
					<img data-v-6effc544=""
						src="">

				</li>
				<li class="step">

					<img data-v-6effc544=""
						src="">
					添加微信
				</li>
				<li class="next">
					<img data-v-6effc544=""
						src="">
				</li>
				<li class="step">

					<img data-v-6effc544=""
						src="">
					查询订单
				</li>
				<li class="next">
					<img data-v-6effc544=""
						src="">
				</li>
				<li class="step">
					<img data-v-6effc544=""
						src="">
					获取结果
				</li>
			</ul>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	import {
		customer,
		getCustomerInfoByOrderNum,
		getDomainDataByDomain,
		ipWxCode,
		measure,
		paymentStatus,
		statistics,
		updatePayStatusByOrderNum,
		uploadConvertData
	} from "@/api/system/system";

	export default {
		data() {
			return {
				form: {},
				poneNumber: null,
				seen: false,
				seens: false,
				orderNumber: null,
				qrcode: 'https://work.weixin.qq.com/kfid/kfc609adbfadb443ab7',
				Url: null,
				bd_vid: null,
				$chou: null,
				payStatus: null,
				myStateInterval: null,
				windowUrl: null,
				loading: false
			}
		},
		onLoad(option) {
			this.orderNumber = localStorage.getItem('orderNumber')
			this.bd_vid = localStorage.getItem('bd_vid')
			this.$chou = localStorage.getItem('$chou')
			//获取详情
			this.getInfos(this.orderNumber)
			//截取Url
			this.getUrl()
		},
		mounted() {
			this.changeStype();
			//判断是电脑还是pc
			this.detectDevice()
		},
		destroyed() {
			//清除定时器
			// clearInterval(this.myStateInterval)
		},
		methods: {
			//生成随机数
			generateUniqueRandomWithSet() {
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				let timestampPart = (Date.now() * Math.random()).toString().slice(0, 10);
				let randomPart = Math.floor(Math.random() * 100000).toString().padStart(5, '0');
				this.orderNumber = `${year}${month}${day}` + timestampPart + randomPart;
				localStorage.setItem('orderNumber', this.orderNumber)
			},
			//获取token
			getToken(url) {
				let params = {
					domainName: url
				}
				getDomainDataByDomain(params).then(res => {
					this.token = res.data.token
				}).catch(() => {})
			},
			//获取当前地址
			getUrl() {
				if (window.location.href.includes('/#')) {
					this.windowUrl = window.location.href.split("/#")[0]
				} else {
					this.windowUrl = window.location.href.split("/?")[0]
				}
				if (this.windowUrl)
					this.getToken(this.windowUrl)
			},
			//循环订单接口
			// forSetInterval() {
			//   this.myStateInterval = setInterval(() => {
			//     this.getStatus()
			//   }, 2000)
			// },
			//查询订单状态
			async getStatus() {
				this.loading = true
				let params = {
					orderNumber: this.orderNumber
				}
				paymentStatus(params).then(res => {
					this.payStatus = res.rows[0].payStatus
					if (this.payStatus == '1') {
						clearInterval(this.myStateInterval)
						// this.updatePayStatusByOrderNums()
						this.getStore()
						this.getData()
					}
					this.loading = false
				}).catch(() => {
					this.loading = false
				})
				return this.payStatus
			},
			//查看测算结果
			getData() {
				if (this.form.sex == 0) {
					this.form.sex = 1
				} else {
					this.form.sex = 0
				}
				let params = {
					username: this.form.customerName,
					gender: this.form.sex,
					date: this.form.brithDate1,
					hour: this.$chou,
					orderNumber: this.orderNumber,
					resulAddress: `http://cs.xuanlingwenhua.com/#/pages/result/index?orderNumber=${this.orderNumber}`
				}
				measure(params).then(res => {}).catch(() => {})
			},
			//百度回传
			getStore() {
				let params = {
					"token": this.token,
					"logidUrl": `${this.windowUrl}?bd_vid=${this.bd_vid}`,
					"newType": "3"
				}
				uploadConvertData(params).then(res => {
					console.log(res.data)
				}).catch((error) => {
					console.log(error)
				})
			},
			//更新用户信息
			updatePayStatusByOrderNums() {
				let params = {
					"requestUrl": document.URL,
					"userName": this.form.customerName,
					"userPhone": '',
					"orderNum": this.form.orderNumber,
					"type": "2",
					"payPrice": "39.8",
					'payStatus': 1
				}
				updatePayStatusByOrderNum(params).then(res => {}).catch(() => {})
			},
			//获取详情
			getInfos(orderNumber) {
				let params = {
					orderNumber: orderNumber
				}
				getCustomerInfoByOrderNum(params).then(res => {
					this.form = res.data
					this.getStatus()
				})
			},
			//新增ip地址与企业微信匹配
			getIp() {
				ipWxCode().then(res => {}).catch(() => {})
			},
			//保存手机号
			async savePhone() {
				var reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
				if (!reg.test(this.poneNumber)) {
					this.$refs.uToast.show({
						message: '请输入正确的手机号！'
					})
					return
				}
				let params = {
					orderNumber: this.orderNumber
				}
				this.loading = true
				await paymentStatus(params).then(res => {
					if (res.rows.length > 0) {
						this.payStatus = res.rows[0].payStatus
						if (this.payStatus == '0') {
							this.updatePayStatusByOrderNumes(this.poneNumber)
							//this.generateUniqueRandomWithSet()
							this.getIp()
							let params = {
								id: this.form.id,
								phone: this.poneNumber
							}
							customer(params).then(res => {}).catch(() => {
								this.loading = false
							})
						}
					}
				}).catch(() => {
					this.loading = false
				})
				setTimeout(() => {
					this.loading = false
					if (this.seen) {
						this.seens = this.seen
					} else {
						window.location.assign('https://work.weixin.qq.com/kfid/kfc609adbfadb443ab7');
					}
				}, 2000)
			},
			//统计-手机号
			updatePayStatusByOrderNumes(poneNumber) {
				let params = {
					"requestUrl": document.URL,
					"userName": this.form.customerName,
					"userPhone": poneNumber,
					"orderNum": this.form.orderNumber,
					"type": "2",
					"payPrice": "39.8",
					'payStatus': 1,
					userAddress: localStorage.getItem('userAddress')
				}
				statistics(params).then(res => {}).catch(() => {})
			},
			//监听手机还是pc
			detectDevice() {
				const userAgent = navigator.userAgent;
				const isMobile = /Mobile|iP(hone|od)|Android/.test(userAgent);
				this.seen = !isMobile;
				if (this.seen) {
					document.querySelector('.contont').style.height = '100%';
				} else {
					document.querySelector('.contont').style.height = '100vh';
				}
			},
			//改变样式
			changeStype() {
              document.body.style.overflow = 'scroll';
			},
		}
	}
</script>
<style lang="scss" scoped>
	.contont {
		margin-right: auto;
		margin-left: auto;
		scroll-behavior: smooth;
		padding-left: 1.3em;
		padding-right: 1.3em;
		padding-top: 0.5em; //1.2
		background: #FFFDF1;
	}

	.tutor-intro {
		display: flex;
	}

	.tutor-intro>p em {
		color: #e05a02;
		font-style: normal;
	}

	.img {
		width: 1rem; //1.5
		height: 1rem; //1.5
	}

	.save-result {
		.heading {
			text-align: center;
			padding-top: .8em;
			padding-bottom: 1.2em;
		}
	}

	.title {
		display: block;
		padding-left: 0.26rem;
		background: url() 0 no-repeat;
		background-size: 0.25rem 0.25rem;
		font-size: .12rem; //0.2
		color: #333;
	}

	.input {
		margin-top: .1rem;
		margin-bottom: 0rem;

		/deep/ .uni-input-input {
			font-size: .16rem !important;
		}

		/deep/ .is-input-border {
			border: 2px solid #a8353a !important;
			border-radius: 1.2rem;
			height: .5rem;

			.uni-input-placeholder {
				font-size: .16rem;

			}
		}
	}

	.smalls {
		width: 100%;
		font-size: .13rem;
		color: #888;
		text-align: center;
	}

	.button {
		background: #a8353a;
		color: #fff;
		font-size: .18rem;
		border: 2px solid #a8353a;
		height: 0.4rem;
		border-radius: 1.2rem;
		box-sizing: border-box;
		margin-top: .11rem;
		margin-bottom: .11rem;
	}

	.icon-agree {
		background: url() 0 no-repeat;
		background-size: .21rem .21rem;
		width: .21rem;
		height: .21rem;
		display: inline-block;
		vertical-align: middle;
	}

	.agree {
		font-size: .14rem;
		color: #888;
		text-align: center;
	}

	.steps {
		margin-top: .5rem;
		padding-bottom: .5rem;
	}

	.steps li {
		display: flex;
		align-items: center;
	}

	.steps ul {
		display: flex;
		justify-content: center;
		list-style-type: none;
		padding: 0;

		.step {
			flex: 1;
			flex-direction: column;
			justify-content: center;
			border: 1px solid #a8353a;
			border-radius: .1rem;
			min-height: .25rem;
			font-size: .1rem;
			color: #a8353a;
			padding-top: .02rem;
			padding-bottom: .02rem;
		}

		.next {
			padding-left: .06rem;
			padding-right: .06rem;
		}
	}

	.steps li.step>img {
		width: .5rem;
		height: .5rem;
	}

	.adress-qr-adress {
		flex-direction: column;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>